package cn.zyjblogs.server.user.service.impl;


import cn.zyjblogs.server.user.dto.UserDto;
import cn.zyjblogs.server.user.dto.UserPageDto;
import cn.zyjblogs.server.user.mapper.UserMapper;
import cn.zyjblogs.server.user.po.UserPo;
import cn.zyjblogs.server.user.service.UserRoleService;
import cn.zyjblogs.server.user.service.UserService;
import cn.zyjblogs.server.user.vo.UserVo;
import cn.zyjblogs.starter.common.entity.context.BaseContext;
import cn.zyjblogs.starter.common.entity.response.HttpCode;
import cn.zyjblogs.starter.common.entity.response.ResponseObject;
import cn.zyjblogs.starter.common.entity.response.ResponseResult;
import cn.zyjblogs.starter.common.utils.bean.BeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author zhuyijun
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPo> implements UserService {
    private final UserMapper userMapper;
    private final UserRoleService userRoleService;

    /**
     * 分页查询
     *
     * @param userPageDto
     * @return com.baomidou.mybatisplus.core.metadata.IPage<cn.zyjblogs.rbac.server.user.vo.UserVo>
     * @author zhuyijun
     * @date 2022/9/19 下午6:52
     */
    @Override
    public IPage<UserVo> findPage(UserPageDto userPageDto) {
        IPage<UserPageDto> page = new Page<>(userPageDto.getPage(), userPageDto.getLimit());
        return userMapper.findPage(page, userPageDto, BaseContext.getTenantId());
    }

    /**
     * 通过用户id查询
     *
     * @param ids
     * @return java.util.List<cn.zyjblogs.rbac.server.user.vo.UserVo>
     * @author zhuyijun
     * @date 2022/9/19 下午6:52
     */
    @Override
    public List<UserVo> findByIds(List<String> ids) {
        List<UserPo> userPos = userMapper.selectBatchIds(ids);
        if (CollectionUtils.isEmpty(userPos)) {
            return Collections.emptyList();
        }
        return BeanUtils.map(userPos, UserVo.class);
    }

    /**
     * @param userDto
     * @return java.lang.Boolean
     * @author zhuyijun
     * @date 2022/9/22 下午6:04
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseObject<String> saveUser(UserDto userDto) {
        UserPo userPo = BeanUtils.map(userDto, UserPo.class);
        int count = userMapper.insert(userPo);
        if (count > 0) {
            userRoleService.saveOrUpdate(userPo.getId(), userDto.getRoleIds(), false);
            return ResponseResult.success("用户创建成功");
        }
        return ResponseResult.error(HttpCode.BAD_REQUEST, "用户创建成功");
    }

    /**
     * 校验用户是否存在
     *
     * @param userDto
     * @return
     */
    @Override
    public boolean checkUser(UserDto userDto) {
        return userMapper.checkUserCount(userDto) > 0;
    }
}
